/* 
 * DOMHelper.java created at: 2005-3-29 21:47:14 by elvis
 * $Id: DOMHelper.java,v 1.1 2006/07/25 08:09:11 wangn Exp $ 
 */

package net.bingosoft.common.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * W3C XML DOM对象操作Helper类 
 * @author elvis
 */
public final class DOMHelper {
    private static final Log log = LogFactory.getLog(DOMHelper.class);
    
    
    /**
     * 
     * @param in 数据文件输入流
     * @return 数据文件Dom
     */
    public static Document parseDocumentOfStream(InputStream in)
    {
        return createDocumentOfStream(in);
    }
    
	/**
     * 
     * @param filePath 数据文件路径
     * @return 数据文件Dom
     */
    public static Document parseDocumentOfFile(String filePath)
    {
        try {
            return parseDocumentOfStream(new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
        	log.error(e);
            throw new RuntimeException(e);
        }
    }
    
    /**
     * create a <code>Document</code> object according to the xml string specified.
     * @param xmlContent
     * @return
     */
    public static Document createDocumentOfXML(String xmlContent){
        Document doc = null;
        if(null != xmlContent){
            try {
                DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                doc = builder.parse(new InputSource(new StringReader(xmlContent)));
            } catch (Exception e) {
                log.error(e);
                throw new RuntimeException("error occurs at createDocumentOfXML",e);
            }
        }
        return doc;
    }
    
    /**
     * create a <code>Document</code> object according to the xml input stream specified.
     * @param xmlstream
     */
    public static Document createDocumentOfStream(InputStream xmlstream) {
        Document doc = null;
        
        if(null != xmlstream){
            try {
                DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                doc = builder.parse(xmlstream);
            } catch (Exception e) {
                log.error(e);
                throw new RuntimeException("error occurs at createDocumentOfStream",e);
            }
        }
        return doc;
    }
    
    /**
     * get element's text value;
     * @param element
     * @return
     */
    public static String getElementText(Element element){
        String text = "";
        if(null != element && element.hasChildNodes()){
            Node node = null;
            NodeList childNodes = element.getChildNodes();
            for(int i=0;i<childNodes.getLength();i++){
                node = childNodes.item(i);
                if(node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE){
                    text = node.getNodeValue();
                }
            }
        }
        return text;
    }
    
    /**
     * get child element by tag name
     * @param parent parent element
     * @param tagName tag name of child element,case sensitivity
     * @return first child element matched the tag name,else null.
     */
    public static Element childElement(Element parent,String tagName){
        Element child = null;
        
        if(parent.hasChildNodes()){
            Node node = null;
            Element element = null;
            NodeList childNodes = parent.getChildNodes();
            for(int i=0;i<childNodes.getLength();i++){
                node = childNodes.item(i);
                if(node.getNodeType() == Node.ELEMENT_NODE){
                    element = (Element)node;
                    if(element.getTagName().equals(tagName)){
                        child = element;
                        break;
                    }
                }
            }
        }
        
        return child;
    }
    
    /**
     * get child elements of parent.
     * @param parent parent element
     * @return child nodes which nodetype equals ELEMENT_NODE
     */
    public static List<Node> childElements(Element parent){
        List<Node> childElements = new ArrayList<Node>();
        
        if(parent.hasChildNodes()){
            Node node = null;
            NodeList childNodes = parent.getChildNodes();
            for(int i=0;i<childNodes.getLength();i++){
                node = childNodes.item(i);
                if(node.getNodeType() == Node.ELEMENT_NODE){
                    childElements.add(node);
                }
            }
        }
        
        return childElements;
    }
    
    /**
     * 
     * @param e
     * @param attr
     * @return
     */
    public static int getIntAttribute(Element e,String attr){
        return StringHelper.toInt(e.getAttribute(attr));
    }
    
    /**
     * 
     * @param e
     * @param attr
     * @return
     */
    public static boolean getBooleanAttribute(Element e,String attr){
        return Boolean.valueOf(e.getAttribute(attr)).booleanValue(); 
    }
}
